﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace CuaHangHoa
{
    public partial class form_quanlymathang : Form
    {
        public form_quanlymathang()
        {
            InitializeComponent();
        }



        public delegate void DoEvent();
        public event DoEvent RefreshDgv;

        public void GetDataFromGridViews(quanlymathang_DTO ds)
        {
            int iRow = dataGridView_qlmh.CurrentRow.Index;
            ds.mamathang = int.Parse(dataGridView_qlmh.Rows[iRow].Cells[0].Value.ToString());
            ds.tenhang = dataGridView_qlmh.Rows[iRow].Cells[1].Value.ToString();
            if (dataGridView_qlmh.Rows[iRow].Cells[2].Value.ToString() == "Đà Lạt")
            {
                ds.xuatxu = 1;
            }
            if (dataGridView_qlmh.Rows[iRow].Cells[2].Value.ToString() == "Đài Loan")
            {
                ds.xuatxu = 2;
            }
            //ds.xuatxu = dataGridView_qlmh.Rows[iRow].Cells[2].Value.ToString();
            ds.donvitinh = dataGridView_qlmh.Rows[iRow].Cells[3].Value.ToString();
            ds.soluongton =int.Parse (dataGridView_qlmh.Rows[iRow].Cells[4].Value.ToString());
            ds.gianhap = int.Parse (dataGridView_qlmh.Rows[iRow].Cells[5].Value.ToString());
            ds.tinhtrang = dataGridView_qlmh.Rows[iRow].Cells[6].Value.ToString();
            ds.ghichu = dataGridView_qlmh.Rows[iRow].Cells[7].Value.ToString();
            ds.dongia  = int.Parse (dataGridView_qlmh.Rows[iRow].Cells[8].Value.ToString());
            if (dataGridView_qlmh.Rows[iRow].Cells[9].Value.ToString() == "Hoa")
            {
                ds.idlloai = 1;
            }
            if (dataGridView_qlmh.Rows[iRow].Cells[9].Value.ToString() == "Chậu")
            {
                ds.idlloai = 2;
            }

        }

        public void Load_mh()
        {
            List<quanlymathang_DTO > ds;
            quanlymathang_BUS bus = new quanlymathang_BUS ();
            ds = bus.LayDanhSachMH();

            dataGridView_qlmh.Rows.Clear();
            for (int i = 0; i <= ds.Count - 1; i++)
            {

                if (ds[i].xuatxu == 1 && ds[i].idlloai  == 1)
                {
                    dataGridView_qlmh.Rows.Add(ds[i].mamathang, ds[i].tenhang, "Đà Lạt", ds[i].donvitinh, ds[i].soluongton, ds[i].gianhap, ds[i].tinhtrang, ds[i].ghichu, ds[i].dongia, "Hoa");
                }

                if (ds[i].xuatxu == 1 && ds[i].idlloai == 2)
                {
                    dataGridView_qlmh.Rows.Add(ds[i].mamathang, ds[i].tenhang, "Đà Lạt", ds[i].donvitinh, ds[i].soluongton, ds[i].gianhap, ds[i].tinhtrang, ds[i].ghichu, ds[i].dongia, "Chậu");
                }


                if (ds[i].xuatxu == 2 && ds[i].idlloai == 1)
                {
                    dataGridView_qlmh.Rows.Add(ds[i].mamathang, ds[i].tenhang, "Đài Loan", ds[i].donvitinh, ds[i].soluongton, ds[i].gianhap, ds[i].tinhtrang, ds[i].ghichu, ds[i].dongia, "Hoa");
                }

                if (ds[i].xuatxu == 2 && ds[i].idlloai == 2)
                {
                    dataGridView_qlmh.Rows.Add(ds[i].mamathang, ds[i].tenhang, "Đài Loan", ds[i].donvitinh, ds[i].soluongton, ds[i].gianhap, ds[i].tinhtrang, ds[i].ghichu, ds[i].dongia, "Chậu");
                }
                
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            form_themmathang frm = new form_themmathang();
            frm.ShowDialog();
        }

        private void quanlymathang_Load(object sender, EventArgs e)
        {
            Load_mh();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                int iRow = dataGridView_qlmh.CurrentRow.Index;
                string ma = dataGridView_qlmh.Rows[iRow].Cells[0].Value.ToString();
                DialogResult dialogResult = MessageBox.Show("Bạn muốn xóa !", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialogResult == DialogResult.Yes)
                {
                    quanlymathang_BUS bus = new quanlymathang_BUS();
                    int kq = bus.XoaMatHang(ma);
                    if (kq == 1)
                    {
                        Load_mh();
                        MessageBox.Show("Xóa thành công");
                    }
                    else
                    {
                        MessageBox.Show("Xóa thất bại");
                    }
                }
                else if (dialogResult == DialogResult.No)
                {
                    return;
                }
            }
            catch (System.Exception ex)
           {
               //errorProvider1.SetError(this.btXo, "Có lỗi");
                MessageBox.Show("Bạn chưa chọn mặt hàng nào !", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
           }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            form_SuaMatHang  frm = new form_SuaMatHang ();
            frm.RefreshDgv += new form_SuaMatHang .DoEvent(Load_mh);
            quanlymathang_DTO ds = new quanlymathang_DTO();
            GetDataFromGridViews(ds);
            frm.Loaddata_gv(ds);
            frm.ShowDialog();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
